$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'TeacherInfo/Select',
        datatype: "json",
        colModel: [
            { label: '职工号', name: 'teacherId', index:'teacher_id',width: 80,key: true },
             { label: '姓名', name: 'teacherName', index:'teacher_name',width: 60 },
             { label: '性别', name: 'teacherSexual',index:'teacher_sexual', width: 40 },
             { label: '出生日期', name: 'teacherBirthday',index:'teacher_birthday', width: 80 },
             { label: '联系电话', name: 'teacherTelNumber',index:'teacher_tel_number', width: 40 },
             { label: '邮箱地址', name: 'teacherEmailAddress',index:'teacher_email_address', width: 40 },
             { label: '所属部门', name: 'teacherCollege', index:'teacher_college',width: 80 },
             { label: '职位', name: 'teacherPosition', index:'teacher_position',width: 40 },
             { label: '职称', name: 'teacherEducation',index:'teacher_education', width: 40 },
             { label: '学历', name: 'teacherTitle',index:'teacher_title', width: 40 },
             { label: '研究方向', name: 'teacherResearchDirection',index:'teacher_research_direction', width: 40 },
             { label: '优先级', name: 'teacherPriority',index:'teacher_priority',width: 40 },
             { label: '课时分布', name: 'teacherClassDistribution',index:'teacher_class_distribution', width: 40 },
             { label: '专业名称', name: 'teacherProfessionName', index:'teacher_profession_name',width: 40 },
             { label: '毕业院校', name: 'teacherGraduatedSchool', index:'teacher_graduated_school',width: 40 },
             { label: '是否有教师资格', name: 'teacherQualifications', index:'teacher_qualifications',width: 40 },
             { label: '人事职工号', name: 'teacherPersonnelId',index:'teacher_personnel_id', width: 40 },
             { label: '是否外聘', name: 'teacherWhetherExternal', index:'teacher_whether_external',width: 40 },
             { label: '是否实验室人员', name: 'teacherWhetherLaboratory',index:'teacher_whether_laboratory', width: 40 },
             { label: '是否在职', name: 'teacherWhetherOnDuty', index:'teacher_whether_on_duty',width: 40 },
             { label: '政治面貌', name: 'teacherPoliticalStatus', index:'teacher_political_status',width: 40 },
             { label: '民族', name: 'teacherEthnic',index:'teacher_ethnic', width: 40 },
             { label: '学位', name: 'teacherDegree', index:'teacher_degree',width: 40 },
             { label: '是否辅导员', name: 'teacherWhetherCounselor',index:'teacher_whether_counselor', width: 40 },
             { label: '拼音简写', name: 'teacherPinyinShort',index:'teacher_pinyin_short', width: 40 }
         ],
        viewrecords: true,
        height:'100%',
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "srcoll" });
        },
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2;
            for(var key in rowData){
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';
            }
            alert(s);
        }
    });
    $("#exportExcel").attr('href',baseURL+'TeacherInfo/export?token='+token);

});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        TeacherInfo: {},
        teacherId: null
    },
    methods: {
        query: function () {
            $("#exportExcel").attr('href',baseURL+'TeacherInfo/export?token='+token+"&teacherName="+vm.q.key);

            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.TeacherInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "TeacherInfo/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.TeacherInfo = r.TeacherInfo;
                vm.teacherId = r.TeacherInfo.teacherId;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "TeacherInfo/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        pullin: function () {
            confirm('确定要开始数据导入？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "Pullin/getTeacherInfoData",
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.TeacherInfo.teacherId == vm.teacherId ? "TeacherInfo/Update" : "TeacherInfo/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.TeacherInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'teacherName': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.TeacherInfo.teacherId)){
                alert("职工号不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherName)){
                alert("姓名不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherSexual)){
                alert("性别不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherBirthday)){
                alert("出生日期不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherTelNumber)){
                alert("联系电话不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherEmailAddress)){
                alert("邮箱地址不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherCollege)){
                alert("所属部门不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherPosition)){
                alert("职位不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherEducation)){
                alert("职称不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherTitle)){
                alert("学历不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherResearchDirection)){
                alert("研究方向不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherPriority)){
                alert("优先级不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherClassDistribution)){
                alert("课时分布不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherProfessionName)){
                alert("专业名称不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherGraduatedSchool)){
                alert("毕业院校不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherQualifications)){
                alert("是否有教师资格不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherPersonnelId)){
                alert("人事职工号不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherWhetherExternal)){
                alert("是否外聘不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherWhetherLaboratory)){
                alert("是否实验室人员不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherWhetherOnDuty)){
                alert("是否在职不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherPoliticalStatus)){
                alert("政治面貌不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherEthnic)){
                alert("民族不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherDegree)){
                alert("学位不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherWhetherCounselor)){
                alert("是否辅导员不能为空");
                return true;
            }
            if(isBlank(vm.TeacherInfo.teacherPinyinShort)){
                alert("拼音简写不能为空");
                return true;
            }
            }
        }
});